#pragma once
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include "Channel.h"
#include "Dispatcher.h"
#include "ChannelMap.h"
#include <pthread.h>
#include <assert.h>
// 前向声明
struct Dispatcher;


// 使用其他文件的全局变量
extern struct Dispatcher EpollDispatcher;
extern struct Dispatcher PollDispatcher;
extern struct Dispatcher SelectDispatcher;
enum ElementType
{
    ADD,
    DELETE,
    MODIFY,
};
// 定义一个链表
struct ChannelElement
{
    int type; // 增加 删除 修改
    struct Channel *channel;
    struct ChannelElement *next;
};

struct EventLoop
{
    bool isQuit;
    struct Dispatcher *dispatcher;
    void *DispatcherData;
    // 任务队列

    struct ChannelElement *head;
    struct ChannelElement *tail;

    // map
    struct ChannelMap *channelMap;

    // 线程ID
    pthread_t threadId;
    // 线程名字
    char threadName[32];

    // c互斥锁
    pthread_mutex_t mutex;

    // 
    int sockfdPair[2]; // 存储本地通信的fd 通过socketpair初始化
};


// 创建主线程
struct EventLoop *CreateEventInit();

// 创建子线程
struct EventLoop *CreateEventInitEx(const char *threadName);

// 启动反应堆

int eventLoopRun(struct EventLoop *evloop);

// 处理激活得事件
int eventActivate(struct EventLoop *evloop, int fd, int events);

// 添加任务到任务队列
// int type是指哪个结构体
int eventAddTask(struct EventLoop *evloop, struct Channel *channel, int type);

// 写数据
void taskWakeUp(struct EventLoop *evloop);

// 写回调函数
int readLocalMessage(void* arg);

// 处理任务队列中的任务

int eventLoopProcess(struct EventLoop *evloop);

// 处理dispatch中的节点

int eventLoopAdd(struct EventLoop *evloop, struct Channel *channel);
int eventLoopDelete(struct EventLoop *evloop, struct Channel *channel);
int eventLoopModify(struct EventLoop *evloop, struct Channel *channel);

// 释放内存
int destoryChannel(struct EventLoop *evloop, struct Channel *channel);


